#include "stdafx.h"
#include "input.h"


void handleSpecial( int key, int x, int y ) {

	switch( key ) {
		case 102:                       // right arrow
			showNewShape(true);
			break;
	}
}

void handleKeyboard( unsigned char key, int x, int y ) {
	vector<float*>::iterator it;
	ofstream file;

	/* keyboard handling */	
	switch( key ) {
		case 'h':
		case 'H':
			// help screen
			SCREEN = HELP;
			break;
		case ' ':
			// game on
			SCREEN = MAIN;
			PREV_TIME = glutGet(GLUT_ELAPSED_TIME)/1000;
			break;
		case 's':
		case 'S':
			// create a new shape file

			file.open("../../shapes/shape.txt");

			for( it = outline.begin(); it < outline.end(); it++ ) {
				float* coords = *it;
				file << coords[0] << " " << coords[1] << endl;
			}
			file << "#" << endl;
			for( it = required.begin(); it < required.end(); it++ ) {
				float* coords = *it;
				file << coords[0] << " " << coords[1] << endl;
			}
			file.close();
			break;
		case 'x':
		case 'X':
			// Restart
			if (MODE == PLAY) {
				LIVES = 0;
				HIT_TIME = 0;
				rasterTimer[DISPLAY_GAMEOVER] = CURR_TIME + 3;
				SCREEN = GAMEOVER;
			} else {
				outline.clear();
				required.clear();
			}
			break;

		case 'q':
		case 'Q':
			// Quit game
			cleanupSound();
			cleanupNetwork();
			exit(0);
			break;

		case 'f':
		case 'F':
			// Toogle between fullscreen and windowed
			if (FULLSCREEN) {
				glutReshapeWindow ( WINDOW_WIDTH, WINDOW_HEIGHT );
				glutPositionWindow( 100, 100 );
			} else {
				glutFullScreen();
			}

			FULLSCREEN = !FULLSCREEN;
			break;

		case 'r':
		case 'R':
			// Call vision server to recalibrate
			recalibrateVision();
			break;
		case 'm':
		case 'M':
			// Toogle background sound
			backgroundSound(!BGMUSIC);
			BGMUSIC = !BGMUSIC;
			break;
		case 'c':
		case 'C':
			// Toogle into CS377S mode
			MONZY = !MONZY;
			break;

	}
}

void handleMouse(int button, int state, int x, int y) {
	if (state == GLUT_DOWN) {
		// convert from pixels to opengl coordinates
		float a = x - WINDOW_WIDTH / 2;
		float b = y - WINDOW_HEIGHT / 2;
		a /= WINDOW_WIDTH / 2;
		b /= WINDOW_HEIGHT / 2;

		// flip the sign of the y-coordinate
		b *= -1;

		float* coordinate = (float*) malloc(sizeof(float) * 3);
		coordinate[0] = a;
		coordinate[1] = b;
		coordinate[2] = 0.f;

		// save until we write out or clear
		if (button == GLUT_LEFT_BUTTON) {
			outline.push_back(coordinate);
		}
		else {
			required.push_back(coordinate);
		}
	}
}
